package com.first.controller;


import com.first.entity.CourseEntity;
import com.first.service.CourseService;
import com.first.util.R;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/courseList")
    public R getCourseList()
    {
        List<CourseEntity> courseList = courseService.getCourseList();
        if(courseList!=null)
        {
            return R.ok().data("list",courseList);
        }
        else
        {
            return R.error();
        }
    }

    @PostMapping("/insert")
    public R insertCourse(String courseTime,Integer courseNumber,Integer courseHour)
    {
        courseService.insertCourse(courseTime,courseNumber,courseHour);
        return R.ok();
    }

    @PostMapping("/update/{courseTime}/{courseNumber}/{courseHour}/{id}")
    public R updateCourse(@PathVariable("courseTime") String courseTime, @PathVariable("courseNumber") Integer courseNumber, @PathVariable("courseHour") Integer courseHour,@PathVariable("id") Integer id)
    {
        courseService.updateCourseTime(courseTime,courseNumber,courseHour,id);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    public R deleteCourse(@PathVariable("id") Integer id)
    {
        int i = courseService.deleteCourse(id);
        if(i>0)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

}
